#pragma once
#include<mutex>
#include"util.hpp"
#include"log.hpp"

//Log log1;
#define CONFID_FILE "./cloud.conf"
namespace cloud
{
    class Config
    {
    private:
        Config()
        {
            ReadConfigFile();
        }
        static Config* _instance;
        static std::mutex _mutex;
    public:
        static Config* GetInstance()
        {
            if(_instance == nullptr)  //懒汉模式
            {
                _mutex.lock();
                if(_instance == nullptr)
                {
                    _instance = new Config();
                }
                _mutex.unlock();
            }
            return _instance;
        }

        //获取热点时间
        int GetHotTime()
        {
            return _hot_time;
        }

        //
        std::string GetServerIp()
        {
            return _server_ip;
        }
        //获取服务端端口号
        int GetServerPort()
        {
            return _server_port;
        }

        //获取下载的url前缀
        std::string GetDownloadPrefix()
        {
            
            return _download_prefix;
        }

        //获取压缩后的文件后缀
        std::string GetPackFileSuffix()
        {
            return _packfile_suffix;
        }

        //获取备份文件的存放目录
        std::string GetPackDir()
        {
            return _pack_dir;
        }

        //获取压缩包的存放目录
        std::string GetBackDir()
        {
            return _back_dir;
        }

        //获取存放数据信息的文件
        std::string GetBackupFile()
        {
            return _backup_file;
        }

        //从配置文件中读取信息并初始化成员变量
        bool ReadConfigFile()
        { 
            Cloud::FileUtil fu(CONFID_FILE);
            std::string body;
            if( fu.GetContent(&body) == false ) //body存的就是配置文件的内容
            {
                //log1(Fatal,"加载配置文件失败");
                std::cout << "load config file failed!\n";
                return false;
            }

            //走到这里配置文件的内容都在body中并且还是个序列化的格式
            Json::Value root;
            if( Cloud::JsonUtil::UnSerialize(body,&root) ) //反序列化
            {
                //log1(Fatal,"反序列化失败");
                return false;
            }
            
            _hot_time = root["hot_time"].asInt();
            _server_port = root["server_port"].asInt();
            _server_ip = root["server_ip"].asString();
            _download_prefix = root["download_prefix"].asString();
            _packfile_suffix = root["_packfile_suffix"].asString();
            _pack_dir = root["pack_dir"].asString();
            _back_dir = root["back_dir"].asString();
            _backup_file = root["backup_file"].asString();
            return true;
        }

    private:
        int _hot_time;
        int _server_port;
        std::string _server_ip;
        std::string _download_prefix;
        std::string _packfile_suffix;
        std::string _pack_dir; //压缩包存放目录
        std::string _back_dir; //存放备份文件的目录
        std::string _backup_file; //存放数据信息的文件
    };

    Config* Config::_instance = nullptr;
    std::mutex Config::_mutex;
}
